import p from "../../../../utils/platform.js";
import api from "../../../../common/api";
import { insertHouseMaterialDownLoadLog } from "../../../../common/log.js";
import util from "../../../../utils/util"
import { httpUrlCheck } from "../../../../utils/utilsNew.js";
Page({
 /**
 * 页面的初始数据
   */
  data: {
    items: [
    ],
    url: util.ImagesUrl,
    keys: [], //一选择站点的id
  },

  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }

    this.setData({
      // items,
      keys: e.detail.value
    })
  },
  saveImageAndroid(img) {
    return new Promise((resolve, reject) => {
      let filePath = wx.env.USER_DATA_PATH + '/' + Date.now() + '.jpg'
      wx.downloadFile({
        url: img,
        filePath,
        success:({filePath}) => {
          wx.saveImageToPhotosAlbum({
            filePath,
            success: () => {
              resolve()
              // 删除缓存
              let fileMgr = wx.getFileSystemManager();
              fileMgr.unlink({
                filePath,
                success: function(r) {
                  console.log("删除缓存ok", r);
                },
              })
            },
            fail: (err) => {
              console.log("保存图片失败", err, filePath);
              resolve()
            }
          })
        },
        fail: () => {
          console.log("读取图片信息失败", err);
          resolve()
        }
      })
    })
  },
   // 下载房源图片
   hanldeDownLoadImgs() {
    if (this.data.keys && this.data.keys.length) {
      insertHouseMaterialDownLoadLog({
        logType: "1",
        delId: this.data.delId
      })

      let list = [...this.data.keys]
      let total = list.length
      let count = 0
      p.showLoading({
        title: `保存中${count}/${total}`
      })

      let save = () => {
        this.saveImageAndroid(list.shift())
          .then(() => {
            p.showLoading({
              title: `保存中${++count}/${total}`
            })

            if (list.length) {
              save()
            } else {
              setTimeout(() => {
                p.hideLoading()
                p.showToast({
                  title: "图片已保存至手机相册",
                  icon: "none",
                })
              }, 200)
              api.getMemberPower({menuId: this.data.menuId},(res) => {
                if(res.data.isSuccess){
                  wx.setStorageSync('memberInfo', res.data.content.rows)
                }
              })
              wx.navigateBack();
            }
          })
      }

      save()
    }
  },
  // 获得vr点位图片
  getVrDataByColumn(){
    api.getVrDataByColumn({
      delId: this.data.delId,
      type: "1"
    }, res => {
      this.setData({
        items: res.data.content.spherePanoramaMaterial.panorama.map((item) => {
          return {
            ...item,
            sphereurl: httpUrlCheck(item.sphereurl) ? item.sphereurl : this.data.url + item.sphereurl + '/750_254_t.jpg',
            sphereurlOrigin: httpUrlCheck(item.sphereurl) ? item.sphereurl : this.data.url + item.sphereurl
          }
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let delId = options.delId;
    let menuId = options.menuId;
    this.setData({
      delId,
      menuId
    })
    this.getVrDataByColumn();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})